package com.ihd.ihardware.view.tzc.me.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemPageBinding;
import com.ihd.ihardware.pojo.OtherUserInfoRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.view.tzc.me.model.MeRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class MyPageViewModel extends AndroidViewModel {

    private MeRepository mMeRepository;

    public MyPageViewModel(@NonNull Application application) {
        super(application);
    }

    public void getOtherUserInfo(String userId) {
        if (mMeRepository == null)
            mMeRepository = new MeRepository();
        mMeRepository.getOtherUserInfo(userId);
    }

    public void getSigns(String userId, int pageNum, int pageSize) {
        if (mMeRepository == null)
            mMeRepository = new MeRepository();
        mMeRepository.getSigns(userId, pageNum, pageSize);
    }

    public void setSigns(List<SignsRes.DataBean.SignDetailsBean> list) {
        mTestDetails.clear();
        if (list != null && list.size() > 0)
            mTestDetails.addAll(list);
    }

    public void setConcern(OtherUserInfoRes.DataBean concern){
        if (concern.isConcern())
            mMeRepository.delConcern(concern.getUserId());
        else
            mMeRepository.concern(concern.getUserId());
    }

    private ObservableArrayList<SignsRes.DataBean.SignDetailsBean> mTestDetails = new ObservableArrayList<>();
    public CommonAdapter mCommonAdapter = new CommonAdapter<SignsRes.DataBean.SignDetailsBean, ItemPageBinding>(R.layout.item_page, mTestDetails, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
        }
    }) {
        @Override
        public void convert(ItemPageBinding mDataBinding, SignsRes.DataBean.SignDetailsBean test_concern, int position) {
            if (!TextUtils.isEmpty(test_concern.getAvatar()))
                Glide.with(mDataBinding.concernedItemLL.getContext())
                        .load(Uri.parse(test_concern.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(mDataBinding.head);
            Glide.with(mDataBinding.concernedItemLL.getContext())
                    .load(Uri.parse((test_concern.getOssFiles() != null && test_concern.getOssFiles().size() > 0) ? test_concern.getOssFiles().get(0) : ""))
                    .into(mDataBinding.bg);
        }
    };

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mMeRepository) {
            mMeRepository.onDestroy();
            mMeRepository = null;
        }
    }
}
